<template>
  <Panel>
    <!-- 标题 -->
    <template #title>修改密码</template>
    <!-- 内容 -->
    <template>
      <el-form
        :model="passForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
        size="mini"
      >
        <el-form-item label="原密码" prop="oldPass">
          <el-input
            type="text"
            v-model="passForm.oldPass"
            autocomplete="off"
            @blur="checkPwd"
          ></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="newPass">
          <el-input
            type="text"
            v-model="passForm.newPass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认新密码" prop="checkPass">
          <el-input
            type="text"
            v-model="passForm.checkPass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >提交</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </template>
  </Panel>
</template>

<script>
import {
  validatePass,
  validateNewPass,
  validCheckPass
} from '@/utils/validate.js'
import { checkOldPass, resetPass } from '@/api/user.js'
import local from '@/utils/local'

export default {
  data() {
    return {
      // 数据对象
      passForm: {
        oldPass: '',
        newPass: '',
        checkPass: ''
      },
      // 规则对象
      rules: {
        oldPass: [{ validator: validatePass, trigger: 'blur' }],
        newPass: [
          { validator: validateNewPass('新密码', this), trigger: 'blur' }
        ],
        checkPass: [
          {
            validator: validCheckPass('确认新密码', this),
            trigger: 'blur'
          }
        ]
      },
      passFlag: false
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          if (this.passFlag) {
            // 发请求
            const { code } = await resetPass({
              newPwd: this.passForm.newPass
            })
            // console.log(data)
            if (code === 0) {
              // 清除token之后刷新一下
              setTimeout(() => {
                local.cls()
                location.reload()
              }, 1500)
            }
          } else {
            this.$message.error('原密码是错误的喔！')
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    async checkPwd() {
      if (this.passForm.oldPass === '') return
      // console.log('asdsa')
      const { code, msg } = await checkOldPass({
        oldPwd: this.passForm.oldPass
      })
      // console.log(data)
      if (code === '00') {
        this.passFlag = true
      } else {
        this.$message({
          type: 'error',
          message: msg
        })
        this.passFlag = false
      }
    }
  }
}
</script>

<style lang="less" scoped>
.el-input,
.el-select {
  width: 300px;
}
</style>
